﻿using AutoMapper;
using ShareFrame.Business.Cache;
using ShareFrame.Entity.Base_BasicData;
using ShareFrame.Entity.Base_Manage;
using ShareFrame.Entity.DTO;
using ShareFrame.IBusiness;
using ShareFrame.Util;
using ShareFrame.Util.ClassLibrary;
using ShareFrame.Util.Helper;
using EFCore.Sharding;
using Microsoft.EntityFrameworkCore;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ShareFrame.IBusiness.Base_Manage;

namespace ShareFrame.Business.Base_Manage
{
    public class HomeBusiness : BaseBusiness<Base_User>, IHomeBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        readonly IMapper _mapper;
        private readonly IBase_UserCache _base_UserCache;
        public HomeBusiness(IDbAccessor db, IOperator @operator, IMapper mapper, IBase_UserCache base_UserCache)
            : base(db)
        {
            _operator = @operator;
            _mapper = mapper;
            _base_UserCache = base_UserCache;
        }

        public async Task<string> SubmitLoginAsync(LoginInputDTO input)
        {
            input.password = input.password.ToMD5String();
            var theUser = await GetIQueryable()
                .Where(x => x.UserName == input.userName && x.Password == input.password)
                .FirstOrDefaultAsync();

            if (theUser.IsNullOrEmpty())
                throw new BusException("账号或密码不正确！");

            return theUser.Id;
        }

        public async Task ChangePwdAsync(ChangePwdInputDTO input)
        {
            var theUser = _operator.Property;
            if (theUser.Password != input.oldPwd?.ToMD5String())
                throw new BusException("原密码错误!");

            theUser.Password = input.newPwd.ToMD5String();
            await UpdateAsync(_mapper.Map<Base_User>(theUser));

            //更新缓存
            await _base_UserCache.UpdateCacheAsync(theUser.Id);
        }


        /// <summary>
        /// Dapper测试案例
        /// </summary>
        /// <param name="basedbOptions"></param>
        /// <returns></returns>
        public async Task<PageResult<Base_Action>> SqlSugarTestAsync(BaseDbOption basedbOptions)
        {
            var dapper = new DapperHelper<Base_Action>(basedbOptions.ConnectionString);
            return await dapper.QueryAsync("select * from Base_Action", null);
        }

        /// <summary>
        /// 统计页面数据统计
        /// </summary>
        /// <param name="basedbOptions">数据库连接对象</param>
        /// <returns></returns>
        public async Task<PageResult<StatisDTO>> GetStatisDataAsync(BaseDbOption basedbOptions)
        {
            var dapper = new DapperHelper<StatisDTO>(basedbOptions.ConnectionString);
            return await dapper.QueryAsync(@"select Count(*) Count from Base_ProjectInfo
                                            union all
                                            select Count(*) Count from Base_VIPUserInfo
                                            union all
                                            select Count(*) Count from Base_VIPUserOrder
                                            union all
                                            select Count Count from Base_AccessNumber where TypeName = '浏览统计'", null);
        }


        ///// <summary>
        ///// MongoDB测试方法-查询数据
        ///// </summary>
        ///// <param name="mdbOptions">MongoDB数据库连接对象</param>
        ///// <param name="input">查询的条件组</param>
        ///// <returns></returns>
        //public async Task<PageResult<Base_Door>> TestMongoDBGetList(MongoDBOptions mdbOptions, PageInput<ConditionDTO> input)
        //{
        //    //实例化MongoDB操作类
        //    MongoDBHelper mongo = new MongoDBHelper(mdbOptions);
        //    //新增实例
        //    List<Base_Door> list = await mongo.FindList<Base_Door>("base_door", input);
        //}


        ///// <summary>
        ///// MongoDB测试方法-添加数据
        ///// </summary>
        ///// <param name="mdbOptions">MongoDB数据库连接对象</param>
        ///// <param name="input">添加的对象实体</param>
        ///// <returns></returns>
        //public async Task TestMongoDBAdd(MongoDBOptions mdbOptions, Base_Door input)
        //{
        //    //实例化MongoDB操作类
        //    MongoDBHelper mongo = new MongoDBHelper(mdbOptions);
        //    //新增实例
        //    await mongo.AddAsync<Base_Door>("base_door", input);
        //}

        ///// <summary>
        ///// MongoDB测试方法-更新数据
        ///// </summary>
        ///// <param name="mdbOptions">MongoDB数据库连接对象</param>
        ///// <param name="input">更新的对象实体</param>
        ///// <returns></returns>
        //public async Task TestMongoDBUpdate(MongoDBOptions mdbOptions, Base_Door input)
        //{
        //    //实例化MongoDB操作类
        //    MongoDBHelper mongo = new MongoDBHelper(mdbOptions);
        //    //新增实例
        //    await mongo.UpdateAsync("base_door", input, input.Id, false);
        //}

        ///// <summary>
        ///// MongoDB测试方法-删除数据
        ///// </summary>
        ///// <param name="mdbOptions">MongoDB数据库连接对象</param>
        ///// <param name="Id">删除的对象ID</param>
        ///// <returns></returns>
        //public async Task TestMongoDBDelete(MongoDBOptions mdbOptions, string Id)
        //{
        //    //实例化MongoDB操作类
        //    MongoDBHelper mongo = new MongoDBHelper(mdbOptions);
        //    //新增实例
        //    await mongo.DeleteAsync<Base_Door>("base_door", Id, false);
        //}

        ///// <summary>
        ///// MongoDB多个id查询
        ///// </summary>
        ///// <param name="mdbOptions"></param>
        ///// <param name="ids"></param>
        ///// <returns></returns>
        //public async Task<PageResult<Base_Door>> TestMongoDBFindIds(MongoDBOptions mdbOptions, List<string> ids)
        //{
        //    //实例化MongoDB操作类
        //    MongoDBHelper mongo = new MongoDBHelper(mdbOptions);
        //    //查询多个实例
        //    var list = await mongo.FindMoreAsync<Base_Door>("base_door", ids);
        //    return new PageResult<Base_Door> { Data = list, Total = list.Count };
        //}

        ///// <summary>
        ///// MongoDB多条件查询
        ///// </summary>
        ///// <param name="mdbOptions"></param>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<PageResult<Base_Door>> TestMongoDBFindMoreCondition(MongoDBOptions mdbOptions, PageInput<List<ConditionDTO>> input)
        //{
        //    //实例化MongoDB操作类
        //    MongoDBHelper mongo = new MongoDBHelper(mdbOptions);
        //    //查询多个实例string collName, FilterDefinition<T> filter, string[] field = null, SortDefinition<T> sort = null
        //    var search = input.Search;

        //    FilterDefinition<Base_Door> filter = Builders<Base_Door>.Filter.Empty;     

        //    foreach (var item in search)
        //    {
        //        filter = filter & Builders<Base_Door>.Filter.Regex(item.Condition, item.Keyword);
        //    }
        //    var list = await mongo.FindListAsync<Base_Door>("base_door", filter);
        //    return new PageResult<Base_Door> { Data = list, Total = list.Count };
        //}
    }
}
